<?php
namespace app\index\controller;

class Doc extends Base
{
    public function index($title ="")
    {
        $category = db('category',[], false)->where('pinyin' , $title)->find();
        //$doclist = db('article_doc' , [] , false)->where('category_id' , $cate)->order('create_time desc')->paginate(22);
        $sub = db('category',[], false)->where('parent_id' , $category['id'])->select();
        //print_r($sub);
        foreach ($sub as $k => $v){
            $sub[$k]['doclist'] = db('article_doc' , [] , false)->where('category_id' , $v['id'])->order('create_time desc')->limit(10)->select();
        }
        //var_dump($category);
        $html_template = 'pic_index';
        if(in_array($category['id'] , [122 , 123])){
            $html_template = 'txt_index';
        }
        return $this->fetchHtml($html_template , [
            'web_title'=> $category['web_title'] . '',
            'web_kw'   => $category['web_kw'],
            'web_desc' => $category['web_desc']. '',
            'category' => $category,
            'sub' => $sub,
        ]);
    }

    public function lists($title ="")
    {
        $category = db('category',[], false)->where('pinyin' , $title)->find();
        $doclist = db('article_doc' , [] , false)->where('category_id' , $category['id'])->order('create_time desc')->paginate(25);
        $parent = db('category',[], false)->where('id' , $category['parent_id'])->find();
        //var_dump($category);
        $html_template = 'pic_lists';
        if(in_array($parent['id'] , [122 , 123])){
            $html_template = 'txt_lists';
        }
        return $this->fetchHtml($html_template , [
            'web_title'=> $category['web_title'] . '',
            'web_kw'   => $category['web_kw'],
            'web_desc' => $category['web_desc']. '',
            'category' => $category,
            'parent' => $parent,
            'doclist' => $doclist,
        ]);
    }

    public function info($id = 0){
        $id = intval($id);
        if($id <= 0){
            $this->redirect('index');
            exit;
        }
        $info = db('article_doc' , [] , false)->where('id' , $id)->find();
        $category = db('category',[], false)->where('id' , $info['category_id'])->find();
        $pre = db('article_doc' , [] , false)->where('category_id' , $category['id'])->where('id' ,'<', $info['id'])->order('create_time desc')->find();
        $next = db('article_doc' , [] , false)->where('category_id' , $category['id'])->where('id' ,'>', $info['id'])->order('create_time desc')->find();
        $aboutlist = db('article_doc' , [] , false)->where('pic!=""')->order('view desc')->limit(12)->select();
        $webtitle = $info['title'];
        return $this->fetchHtml('info',[
            'web_title' =>  $webtitle . ' - ' . $category['title'] . '',
            'web_kw' => $webtitle,
            'web_desc' => $webtitle,
            'info' => $info,
            'category' => $category,
            'pre' => $pre,
            'next' => $next,
            'aboutlist' => $aboutlist
        ]);
    }


    public function tags(){
        //防止$title的sql注入
        $title = input('title');
        $title = addslashes($title);
        $doclist = db('article_doc' , [] , false)->where('kw like "%' . $title . '%"')->order('create_time desc')->paginate(25);

        return $this->fetchHtml('tag',[
            'web_title' =>  ' - ' . $title . '',
            'web_kw' => $title,
            'web_desc' => $title,
            'doclist' => $doclist,
            'title' => $title,
        ]);
    }

}
